package com.bulletelc.apollopre.module.main.center.intention.list;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.IntentionBean;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018/5/16.
 */

public class IntentionListPresenter extends BasePresenter<IntentionListContract.View> implements IntentionListContract.Presenter {

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void getIntentionList() {
        getIntentListImp();
    }

    private void getIntentListImp() {
        String json = "{\n" + "\t\"state\":[15, 20]\n" + "}";
        final RequestBody requestBody = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON), json);
        OkGo.<String>post(NetWorkPath.POST_INTENTION_LIST)
                .headers(NetWorkPath.HEADER_KEY_AUTH, NetWorkPath.AUTH_TOKEN_PRE + MyApplication.getToken())
                .upRequestBody(requestBody)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseList(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorIntentionList(response.getException().toString());
                        }
                    }
                });
    }

    private void parseList(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject object = new JSONObject(response.body().toString());
                JSONObject meta = object.getJSONObject("meta");
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(meta.getString("code"))) {
                    Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
                        @Override
                        public boolean shouldSkipField(FieldAttributes f) {
                            return f.getName().contains("_links");
                        }

                        @Override
                        public boolean shouldSkipClass(Class<?> clazz) {
                            return false;
                        }
                    }).create();
                    Type type = new TypeToken<List<IntentionBean>>() {
                    }.getType();
                    List<IntentionBean> intentionBeanList = gson.fromJson(object.getJSONArray("data").toString(), type);
                    if (isViewAttached()) {
                        getView().onSuccessIntentionList(intentionBeanList);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailIntentionList(meta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            if (isViewAttached()) {
                getView().onFailIntentionList(response.body().toString());
            }
        }
    }

}
